package com.ylkj.patient.moduleimpl;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.examination.mlib.constants.ARouterConstants;
import com.examination.mlib.interfaceapi.DrugListBackApi;
import com.examination.mlib.interfaceapi.MainCallBack;
import com.ylkj.patient.MainApplication;
import com.ylkj.patient.rnmodule.RNRouterModule;
import com.ylkj.patient.ui.activities.MainActivity;
import com.ylkj.patient.utils.AMapLocationUtils;
import com.ylkj.patient.utils.AppReactUtils;

import java.util.HashMap;

@Route(path = ARouterConstants.DrugListModuleImpl)
public class DrugListModuleImpl implements DrugListBackApi {

    /**
     * 无参android to rn
     * @param context
     * @param pluginId rn包名
     * @param componentName rn页面名
     */
    @Override
    public void Android2RnPush(Context context, String pluginId, String componentName) {
        AppReactUtils.INSTANCE.push(context,pluginId,componentName);
    }

    /**
     * 带参androi to rn
     * @param context
     * @param pluginId rn包名
     * @param componentName rn页面名
     * @param bundle 参数
     */
    @Override
    public void Android2RnPush(Context context, String pluginId, String componentName, Bundle bundle) {
        AppReactUtils.INSTANCE.push(context,pluginId,componentName,bundle);
    }

    @Override
    public void changeHomeFragment(Activity activity) {
        if (activity != null && activity instanceof  MainActivity){
            ((MainActivity) activity).viewpagerChange(0,0);
        }
//        MainActivity.viewpagerChange(0,0);

    }

    @Override
    public void saveLocalAddress(Context context) {
        AMapLocationUtils.saveLocalInfo(context);
    }

    @Override
    public void getLocalAddress(MainCallBack mainCallBack) {
        AMapLocationClientOption mLocationOption = new AMapLocationClientOption();
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
        mLocationOption.setOnceLocation(true);
        mLocationOption.setOnceLocationLatest(true);
        //初始化定位
        AMapLocationClient mLocationClient = new AMapLocationClient(MainApplication.getContext());
        AMapLocationListener mAMapLocationListener = new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation amapLocation) {
                if (amapLocation != null) {

                    if (amapLocation.getErrorCode() == 0) {
//可在其中解析amapLocation获取相应内容。
//                        amapLocation.getLatitude();//获取纬度
//                        amapLocation.getLongitude();//获取经度
//                        amapLocation.getCity();//城市信息
//                        JSONArray newArray = new JSONArray();
//                        newArray.put(amapLocation.getCity());
//                        newArray.put(amapLocation.getLatitude()+"");
//                        newArray.put(amapLocation.getLongitude()+"");
//                        String address = amapLocation.getAddress();
//                        String province = amapLocation.getProvince();
//                        String city = amapLocation.getCity();
//                        String district = amapLocation.getDistrict();
//                        String street = amapLocation.getStreet();
//                        double latitude = amapLocation.getLatitude();
//                        double longitude = amapLocation.getLongitude();
//                        mainCallBack.onSuccess(address);
                        mainCallBack.onSuccess(amapLocation);
                    }else {
                        mainCallBack.onError(amapLocation.getErrorInfo());
                    }
                }else {
                    //定位失败时，可通过ErrCode（错误码）信息来确定失败的原因，errInfo是错误信息，详见错误码表。
//                    Toast.makeText(getApplicationContext(),"定位获取失败,请检查是否开启定位权限",Toast.LENGTH_SHORT).show();

                }
            }
        };
//设置定位回调监听
        mLocationClient.setLocationListener(mAMapLocationListener);
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
//启动定位
        mLocationClient.startLocation();
    }

    @Override
    public HashMap<String, String> getTemporaryDataStorage() {
        return RNRouterModule.getTemporaryDataStorage();
    }



    @Override
    public void init(Context context) {

    }
}
